package org.lmind.scale.asyn

import scala.actors.Actor
import java.util.ArrayList
import java.util.Iterator

class AsynSerialization[T] {

  case class Fn(method: ((Boolean, Any) => Unit) => Unit, callback: (Boolean, Any) => Any) {
  }

  var list: ArrayList[Fn] = new ArrayList()

  def add(method: ((Boolean, Any) => Unit) => Unit, fn: (Boolean, Any) => Any) = {
    list.add(new Fn(method, fn))
  }

  def run() = {
    
    var n: (Iterator[AsynSerialization.this.Fn]) => Unit = null

    n = (iter: java.util.Iterator[AsynSerialization.this.Fn]) => {
      var x = iter.next
      x.method((success, data) => {
        x.callback(success, data)
        if (success) {
          n(iter)
        }
      })
    }
    n(list.iterator)
  }

}

object Test {

  class AsynOper {

    def fn1 = {
      new AsynResult[Int](1);
    }
    def fn2 = {
      new AsynResult[Int](2);
    }
    def fn3 = {
      new AsynResult[Int](3);
    }

    def fn4(a: Int, b: Int)(fn: (Boolean, Any) => Unit) = {
      fn(a + b > 0, a + b)
    }
  }

  def main(args: Array[String]) {
    

    val service = new AsynOper();
    var t = {
      service.fn1;
    }

    val a = 12;
    var b = 13;

    val asyn = new AsynSerialization[Int]()

    val aa = service.fn4(1, 2)_
    aa((success, data) => success)
    

//    asyn.add(service.fn4(1, 2)_, {
//      case (false, data) => println("false")
//      case (true, data) => println(data)
//    })
//    asyn.add(service.fn4(-13, 2)_, {
//      case (false, data) => println("false")
//      case (true, data) => println(data)
//    })
//    asyn.add(service.fn4(3, 4)_, {
//      case (false, data) => println("false")
//      case (true, data) => println(data)
//    })
//
    asyn.run
  }
}